/*
 * JerboApp.java
 */

package jerbo;

import com.sun.rowset.CachedRowSetImpl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.rowset.CachedRowSet;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class JerboApp extends SingleFrameApplication {

    private Connection conn = null;
    
    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        show(new JerboView(this));
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     * @param root 
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of JerboApp
     */
    public static JerboApp getApplication() {
        return Application.getInstance(JerboApp.class);
    }

    /**
     * Main method launching the application.
     * @param args 
     */
    public static void main(String[] args) {
        launch(JerboApp.class, args);
    }
    
    public Connection getConnection() throws ClassNotFoundException, SQLException {
        if (conn == null) {
            org.jdesktop.application.ResourceMap resourceMap = getContext().getResourceMap(JerboApp.class);
            Class.forName(resourceMap.getString("Application.JDBCDriver"));
            conn = DriverManager.getConnection(resourceMap.getString("Application.connectionString"));
        }
        return conn;
    }
    
    /**
     * Caches a ResultSet in a CachedRowSet
     * 
     * @param rs input ResultSet to be cached
     * @return ResultSet (CachedRowSet)
     * @throws java.sql.SQLException 
     */
    public ResultSet cacheResultSet(ResultSet rs) throws SQLException {
        CachedRowSet crs = new CachedRowSetImpl();
        crs.populate(rs);
        return crs;
    }
    
}
